package com.eastfair.auth.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.service.*;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.Organization;
import com.eastfair.core.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 项目组织业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Service
//@Transactional
@DS("#thread.tenant")
public class ProjectOrganizationServiceImpl implements ProjectOrganizationService {

    @Autowired
    OrganizationService organizationService;


    @Autowired
    ResourceOrganizationService resourceOrganizationService;

    @Autowired
    CompanyOrganizationService companyOrganizationService;

    @Autowired
    AudienceOrganizationService audienceOrganizationService;

    @Autowired
    SponsorUserOrganizationService sponsorUserOrganizationService;

    @Override
    public R<Organization> createProject(OrganizationDTO organizationDTO) {
        //校验已经是否存在
        Organization organizationClassify = organizationService.queryOrgOfType(organizationDTO.getProjectId(), OrganizationOrgTypeEnum.PROJECT);
        if (organizationClassify != null) {
            //存在则直接返回
            return R.success(organizationClassify);
        }
        //创建项目组织
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.PROJECT);
        R<Organization> r = organizationService.createOrg(organizationDTO);
        Organization organization = r.getData();
        //公司组
        companyOrganizationService.createCompanyClassify(organization.getId(), organization.getProjectId());
        //创建项目下的公司分组
        companyOrganizationService.createCompanyClassifyOfProject(organization.getId(), organization.getProjectId());
        //创建项目下的观众分组
        audienceOrganizationService.createAudienceClassify(organization.getId(), organization.getProjectId());
        //主办组
        sponsorUserOrganizationService.createSponsorClassify(organization.getId(), organization.getProjectId());
        //创建项目下的主办分组
        sponsorUserOrganizationService.createSponsorClassifyOfProject(organization.getId(), organization.getProjectId());
        return R.success(organization);
    }

    @Override
    public R<Organization> updProject(OrganizationDTO organizationDTO) {
        //校验已经是否存在
        Organization organizationClassify = organizationService.queryOrgOfType(organizationDTO.getProjectId(), OrganizationOrgTypeEnum.PROJECT);
        if (organizationClassify != null) {
            //修改节点名字
            organizationClassify.setName(organizationDTO.getName());
            organizationService.updateByIdOfShard(organizationClassify);
            //存在则直接返回
            return R.success(organizationClassify);
        }
        return R.success(null);
    }

    @Override
    public R delProject(Long projectOrgId) {
        return organizationService.del(projectOrgId);
    }


}
